package cn.smbms.service.user;

import java.util.List;

import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String userCode, String userPassword) throws Exception {
        User user = null;
        user = userMapper.getLoginUser(userCode);
        //匹配密码
        if(null != user){
            if(!user.getUserPassword().equals(userPassword))
                user = null;

        }
        return user;
    }

    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {

        int count = 0;
        System.out.println("queryUserName ---- > " + queryUserName);
        System.out.println("queryUserRole ---- > " + queryUserRole);
        count = userMapper.getUserCount(queryUserName,queryUserRole);
        return count;
    }

    public List<User> getUserList(String queryUserName, int queryUserRole,
                                  int currentPageNo, int pageSize) {
        int beginIndex = (currentPageNo-1)*pageSize;
        return userMapper.getUserList(queryUserName, queryUserRole, beginIndex,
                pageSize);
    }

}
